//
//  LXLanguageViewController.m
//  INMOTION_CustomMadeProject
//
//  Created by cjg on 2021/3/23.
//  Copyright © 2021 cjg. All rights reserved.
//

#import "LXLanguageViewController.h"
#import "NSObject+HUD.h"
#import "KSLanguageManager.h"
#import "LXLanguageCell.h"

@interface LXLanguageViewController ()

@property (nonatomic , strong) NSMutableArray *languages;

@end

@implementation LXLanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"语言";
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = left;
    
    self.languages = [NSMutableArray arrayWithArray:@[IMTLocalizedStringFromTable(@"SystemDefault", @"Me", @"系统"),kIMTSimplifiedChineseLanguageName,kIMTTraditionalChineseLanguageName,kIMTEnglishLanguageName,kIMTFrenchLanguageName]];
    [self.tableView registerNib:[UINib nibWithNibName:@"LXLanguageCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView reloadData];

}

- (void)backAction {
    [self dismissHUD];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.languages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LXLanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSLog(@"%@",[[KSLanguageManager sharedInstance] currentLanguageOriginal]);
    cell.titleLB.text = self.languages[indexPath.row];
    if ([cell.titleLB.text isEqualToString:[[KSLanguageManager sharedInstance] currentLanguageName]]) {
        cell.checkIM.hidden = NO;
    }else {
        cell.checkIM.hidden = YES;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        [[KSLanguageManager sharedInstance] setUseSystemLanguage:YES];
    } else {
        NSString *item = self.languages[indexPath.row];
        [[KSLanguageManager sharedInstance] setUseSystemLanguage:NO];
        [[KSLanguageManager sharedInstance] setLanguageWithLanguageName:item];
    }
    [self.tableView reloadData];
}

@end
